﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ASBCC.Workflow.Client
{
    public class ActivityLink
    {
        private ActivityLinkNode _firstNode;

        private ActivityLinkNode _currentNode;

        public ActivityLink()
        {
        }

        public ActivityLink(string[] activityArr)
        {
            if (activityArr == null) return;
            foreach (string activity in activityArr)
            {
                AddLast(activity);
            }
        }

        public ActivityLinkNode First
        {
            get
            {
                _currentNode = _firstNode;
                return _currentNode;
            }
        }

        public ActivityLinkNode AddLast(string value)
        {
            if (_currentNode == null)
            {
                _firstNode = new ActivityLinkNode(value);
                _currentNode = _firstNode;
                return _currentNode;
            }
            else
            {
                _currentNode = _currentNode.AddLastChild(value);
                return _currentNode;
            }
        }

        public ActivityLinkNode FindNode(string value)
        {
            ActivityLinkNode current = First;
            if (current == null) return null;
            do
            {
                if (current.Value == value) return current;
                current = current.LastChild;
                
            } while (current != null);
            return null;
        }
    }

    public class ActivityLinkNode
    {
        private string _value;
        public string Value { get { return _value; } set { _value = value; } }

        private List<ActivityLinkNode> _nodes;
        public List<ActivityLinkNode> Nodes
        {
            get
            {
                if (_nodes == null)
                {
                    _nodes = new List<ActivityLinkNode>();
                }
                return _nodes;
            }
            set
            {
                _nodes = value;
            }
        }

        public ActivityLinkNode() { }

        public ActivityLinkNode(string value)
        {
            _value = value;
        }

        public ActivityLinkNode AddLastChild(string value)
        {
            ActivityLinkNode childNode = new ActivityLinkNode(value);
            return AddLastChild(childNode);
        }

        public ActivityLinkNode AddLastChild(ActivityLinkNode childNode)
        {
            this.Nodes.Add(childNode);
            return childNode;
        }

        public ActivityLinkNode LastChild
        {
            get
            {
                if (Nodes.Count == 0) return null;
                return Nodes[Nodes.Count - 1];
            }
        }
    }
}
